package com.example.ymm.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import java.io.IOException;
import java.text.SimpleDateFormat;

@Slf4j
public class JsonUtil {
    public static JSONObject parseEntityToJson(Object object){
            ObjectMapper mapper = new ObjectMapper();
            String jsonStr = "";
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                mapper.setDateFormat(format);
                jsonStr = mapper.writeValueAsString(object);
            } catch (IOException e) {
               return null;
            }
            return JSONObject.parseObject(jsonStr);
    }

    public static String parseEntityToJsonString(Object object){
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = "";
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mapper.setDateFormat(format);
            jsonStr = mapper.writeValueAsString(object);
        } catch (IOException e) {
            return null;
        }
        return jsonStr;
    }

    public static <T> T parseJsonToEntity(String JsonString, Class<T> clazz){
        try{
            JSONObject temp = JSONObject.parseObject(JsonString);
            return JSONObject.toJavaObject(temp,clazz);
        } catch (Exception e){
            log.error("add message err:",e);
            return null;
        }
    }

    public static boolean isJson(String content){

        try {
            JSONObject jsonStr= JSONObject.parseObject(content);
            return  true;
        } catch (Exception e) {
            return false;
        }
    }



}
